package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.NewscommentMapper;
import com.kgc.mapper.NewsdetailMapper;
import com.kgc.pojo.Newscomment;
import com.kgc.pojo.Newsdetail;
import com.kgc.pojo.NewsdetailExample;
import com.kgc.service.NewsdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class NewsdetailServiceImpl implements NewsdetailService {

    @Autowired
    private NewsdetailMapper newsdetailMapper;

    @Autowired
    private NewscommentMapper newscommentMapper;

    public List<Newsdetail> selectNewsdetailAll() {
        return newsdetailMapper.selectByExample(null);
    }

    public Newsdetail selectNewsdetailById(Integer id) {
        Newsdetail newsdetail=newsdetailMapper.selectByPrimaryKey(id);

        if(null==newsdetail){
            return null;
        }
        return newsdetail;
    }



    //分页查询
    @Transactional//事务
    public PageInfo<Newsdetail> selectNewsdetailByCondition(String title, Integer pageNo, Integer pageSize) {
        NewsdetailExample newsdetailExample=new NewsdetailExample();
        NewsdetailExample.Criteria newsdetailExampleCriteria=newsdetailExample.createCriteria();

        if(null!=title&&!title.equals("")){
            newsdetailExampleCriteria.andTitleLike("%"+title+"%");
        }
        PageHelper.startPage(pageNo,pageSize);

        //进行查询
        List<Newsdetail> newsdetails=newsdetailMapper.selectByExample(newsdetailExample);
        if(null==newsdetails||newsdetails.size()==0){
            return null;
        }


        //分页操作
        PageInfo<Newsdetail> pageInfo=new PageInfo<Newsdetail>(newsdetails);
        return pageInfo;
    }

    public boolean deleteNewsdetail(Integer id) {
        return newsdetailMapper.deleteByPrimaryKey(id)>0?true:false;
    }
}
